package day04;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

@WebServlet("*.code")
public class CodeServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String path = request.getServletPath();

        if ("/show.code".equals(path)) {
            show(request,response);
        }

        if ("/submit.code".equals(path)) {
            submit(request,response);
        }

    }
    protected void submit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CodeServlet.submit()");
        HttpSession session=request.getSession();
        String code=request.getParameter("code");
        String sessionCode= (String) session.getAttribute("code");

        if(code!=null&&!"".equals(code.trim())&&code.equalsIgnoreCase(sessionCode)){
            session.removeAttribute("code");

            response.sendRedirect(request.getContextPath()+"/hello.jsp");
            return;
        }


        request.setAttribute("message", "验证码错误");

        request.getRequestDispatcher("/code.jsp").forward(request,response);

    }


    protected void show(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("CodeServlet.show()");

        HttpSession session=request.getSession();

        Random random=new Random();

        //在内存中创建一个画图板
        BufferedImage image=new BufferedImage(70,30,BufferedImage.TYPE_INT_RGB);


        //获得画笔
        Graphics graphics=image.getGraphics();


        //设置画笔颜色
        graphics.setColor(new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)));


        //画一个带填充色的矩形作为背景
        graphics.fillRect(0,0,70,30);

        //设置画笔颜色
        graphics.setColor(new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)));

        //设置画笔字体
        graphics.setFont(new Font("Courier New",Font.BOLD+Font.ITALIC,18));

        //生成一个5位的随机字符串，字符串中包括[a-z][A-Z][0-9]
        String s="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuffer code=new StringBuffer();
        for (int i = 0; i < 5; i++) {
            int index=random.nextInt(s.length());
            code.append(s.charAt(index));
        }

        session.setAttribute("code",code.toString());

        //将文字写到图片上
        graphics.drawString(code.toString(),5,20);

        //画3条干扰线
        for (int i = 0; i < 3; i++) {
            //设置画笔颜色
            graphics.setColor(new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)));
            graphics.drawLine(random.nextInt(71),random.nextInt(31),random.nextInt(71),random.nextInt(31));
        }


        //将图片压缩成JPEG格式发送到客户端
        OutputStream out=response.getOutputStream();
        ImageIO.write(image,"jpeg",out);
    }
}
